package TcpUdpDemp;

import javax.xml.crypto.Data;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

import static com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver.length;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("===好哦好哦，好呀好呀===");
        //1.创建服务端对象,注册端口
        DatagramSocket socket = new DatagramSocket(6666);
        //2.创建数据包对象，接受数据
        byte[] buffer = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
        //3.正式使用数据包 来接受客户端发来的数据
        while (true) {
            socket.receive(packet);
            //4.从字节数组中，把接受到的数据直接打印出来
            //接受多少数据要倒出多少数据
            //获取数据包大小
            int length = packet.getLength();

            String rs = new String(buffer,0,length);
            System.out.println("来自"+packet.getAddress().getHostName()+"\r\n"+rs);

            System.out.println(packet.getAddress().getHostAddress());
            System.out.println(packet.getPort());
            System.out.println("---------------------");
//        socket.close(); 服务端不会死亡，所以不应该关闭
        }
    }
}
